/******************************************************************
Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
* @projectName   01_smarthome
* @brief         mainwindow.cpp
* @author        Deng Zhimao
* @email         1252699831@qq.com
* @net           www.openedv.com
* @date          2021-05-26
*******************************************************************/
#include "mainwindow.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/signal.h>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 界面布局 */
    layoutInit();
    /*退出按钮*/
    exitPushButton = new QPushButton(this);
    exitPushButton->move(15, 10);             /*设置显示位置*/
    exitPushButton->setMinimumHeight(42);
    exitPushButton->setMinimumWidth(150);
    exitPushButton->setFocusPolicy(Qt::NoFocus);
    exitPushButton->setObjectName("exitPushButton");
    exitPushButton->setText("Destiny");
    QIcon icon(":/icons/exit.png");
    exitPushButton->setIcon(icon);
    exitPushButton->setIconSize(QSize(42, 42));
    connect(exitPushButton,SIGNAL(clicked()),this, SLOT(exitPushButtonCliked()));


    fd_dht11 = open(DHT11_DEV_PATH,O_RDONLY);
    if(fd_dht11 < 0)
    {
        qDebug()<<"can't open "<<DHT11_DEV_PATH<<endl;
    }

    fd_led = open(LED_DEV_PATH,O_RDWR);
    if(fd_led < 0)
    {
        qDebug()<<"can't open "<<LED_DEV_PATH<<endl;
    }

    timerReadData = new QTimer();
    timerReadData->setInterval(1000);
    connect(timerReadData,SIGNAL(timeout()),this,SLOT(timerOut()));
    for (int i = 0; i < 3; i++)
        connect(switchButton[i], SIGNAL(toggled(bool)),
                this, SLOT(onToggled(bool)));
    timerReadData->start();
}
MainWindow::~MainWindow()
{
}

void MainWindow::layoutInit()
{
    /* 主界面背景 */
    this->setGeometry(0, 0, 800, 480);
    this->setMinimumWidth(700);
    this->setMinimumHeight(450);
    this->setObjectName("smarthome");

    for (int i = 0; i < 11; i++) {
        widget[i] = new QWidget();
        widget[i]->setObjectName(tr("widget%1")
                                 .arg(QString::number(i)));
    }
    /*垂直布局*/
    for (int i = 0; i < 2; i++)
        vBoxLayout[i] = new QVBoxLayout();
    /*水平布局*/
    for (int i = 0; i < 6; i++)
        hBoxLayout[i] = new QHBoxLayout();
    /*显示部件*/
    for (int i = 0; i < 10; i++) {
        label[i] = new QLabel();
        label[i]->setObjectName(tr("label%1")
                                .arg(QString::number(i)));
    }
//      /*  主页面布局，分为五个部分*/
    widget[1]->setMaximumHeight(40);
    widget[2]->setMaximumHeight(25);
    widget[3]->setMaximumHeight(150);
    widget[4]->setMaximumHeight(30);
    widget[5]->setMaximumHeight(150);
    vBoxLayout[0]->addWidget(widget[1]);   /*信息*/
    vBoxLayout[0]->addWidget(widget[2]);
    vBoxLayout[0]->addWidget(widget[3]);
    vBoxLayout[0]->addWidget(widget[4]);
    vBoxLayout[0]->addWidget(widget[5]);
    vBoxLayout[0]->setContentsMargins(80, 52, 80, 50);

    /* 主界面采用垂直布局 */
    widget[0]->setLayout(vBoxLayout[0]);
    setCentralWidget(widget[0]);

    hBoxLayout[0]->addWidget(label[0]);
    hBoxLayout[0]->setContentsMargins(0, 0, 0, 0);
    label[0]->setText("基于IMX6ULL的测试程序");
    widget[1]->setLayout(hBoxLayout[0]);

    hBoxLayout[1]->addWidget(label[1]);
    label[1]->setText("DHT11收集温湿度、esp8266接入mqtt服务器");
    hBoxLayout[1]->setContentsMargins(0, 0, 0, 0);
    widget[2]->setLayout(hBoxLayout[1]);

    hBoxLayout[2]->addWidget(label[2]);
    hBoxLayout[2]->setContentsMargins(0, 0, 0, 0);
    label[2]->setText("常用控制面板");
    widget[4]->setLayout(hBoxLayout[2]);

    /*控制面板内容*/
    vBoxLayout[1]->addWidget(widget[6]);
    vBoxLayout[1]->addWidget(widget[7]);
    vBoxLayout[1]->setContentsMargins(0, 0, 0, 0);
    widget[3]->setLayout(vBoxLayout[1]);   /*垂直布局*/
    /*温湿度数据*/
    QStringList list;
    list<<"温度："<<"25.0"<<"湿度："<<"50.0";
    QStringList iconListForDht11;
    iconListForDht11<<":/icons/temp.png"<<":/icons/humility.png";

    int j = 0;
    for (int i = 3; i < 5; i++) {
        label[i]->setFixedSize(100, 50);
        label[i]->setFocusPolicy(Qt::NoFocus);
        label[i]->setPixmap(QPixmap(iconListForDht11[j++]).scaled(100, 50, Qt::KeepAspectRatio));
    }
    j = 0;
    for (int i = 5; i < 9; i++) {
        label[i]->setFixedSize(100, 50);
        label[i]->setFocusPolicy(Qt::NoFocus);
        label[i]->setText(list[j++]);
    }

    hBoxLayout[3]->addWidget(label[9]);
    hBoxLayout[3]->setAlignment(Qt::AlignCenter);
    hBoxLayout[3]->setSpacing(100);
    hBoxLayout[3]->setContentsMargins(0, 0, 0, 0);
    label[9]->setText("DHT11温湿度数据");
    widget[6]->setLayout(hBoxLayout[3]);

    hBoxLayout[4]->addWidget(label[3]);
    hBoxLayout[4]->addWidget(label[5]);
    hBoxLayout[4]->addWidget(label[6]);
    hBoxLayout[4]->addWidget(label[4]);
    hBoxLayout[4]->addWidget(label[7]);
    hBoxLayout[4]->addWidget(label[8]);
    hBoxLayout[4]->setAlignment(Qt::AlignCenter);
    hBoxLayout[4]->setContentsMargins(50, 0, 0, 0);
    widget[7]->setLayout(hBoxLayout[4]);


    /* 控制列表 */
    listWidget = new QListWidget();
    listWidget->setFocusPolicy(Qt::NoFocus);
    listWidget->setMaximumHeight(150);
    listWidget->setVerticalScrollBarPolicy(
                Qt::ScrollBarAlwaysOff);
    listWidget->setHorizontalScrollBarPolicy(
                Qt::ScrollBarAlwaysOff);
    /* 横向排布 */
    listWidget->setFlow(QListView::LeftToRight);
    hBoxLayout[5]->addWidget(listWidget);
    hBoxLayout[5]->setContentsMargins(0, 0, 0, 0);
    widget[5]->setLayout(hBoxLayout[5]);

    QStringList iconList;
    iconList<<":/icons/light.png"
           <<":/icons/windows.png"
          <<":/icons/fan.png";
    QStringList localtionList;
    localtionList<<"灯"<<"窗帘"<<"空调";
    QStringList nameList;
    nameList<<"灯|关"<<"窗帘|关"<<"空调|关";

    /* 布局在前面章节详细说过，不再一一注释 */
    for (int i = 0; i < 3; i++) {
        gridLayout[i] =  new QGridLayout();
        gridLayout[i]->setContentsMargins(0, 0, 0, 0);
    }

    for (int i = 0; i < 3; i++) {
        switchButton[i] =  new SwitchButton();
        switchButton[i]->setMaximumSize(50, 30);
    }

    for (int i = 0; i < 9; i++) {
        itemLabel[i] = new QLabel();
        itemLabel[i]->setMaximumSize(75, 75);
        itemLabel[i]->setScaledContents(true);
        itemLabel[i]->setAlignment(Qt::AlignCenter);
        itemLabel[i]->setObjectName(tr("itemLable%1")
                                    .arg(QString::number(i)));
    }

    itemLabel[0]->setPixmap(QPixmap(iconList[0]));
    itemLabel[1]->setPixmap(QPixmap(iconList[1]));
    itemLabel[2]->setPixmap(QPixmap(iconList[2]));

    itemLabel[3]->setText(localtionList[0]);
    itemLabel[4]->setText(localtionList[1]);
    itemLabel[5]->setText(localtionList[2]);

    itemLabel[6]->setText(nameList[0]);
    itemLabel[7]->setText(nameList[1]);
    itemLabel[8]->setText(nameList[2]);

    gridLayout[0]->addWidget(itemLabel[0], 0, 0);
    gridLayout[0]->addWidget(itemLabel[3], 0, 1);
    gridLayout[0]->addWidget(itemLabel[6], 1, 0);
    gridLayout[0]->addWidget(switchButton[0], 1, 1);

    gridLayout[1]->addWidget(itemLabel[1], 0, 0);
    gridLayout[1]->addWidget(itemLabel[4], 0, 1);
    gridLayout[1]->addWidget(itemLabel[7], 1, 0);
    gridLayout[1]->addWidget(switchButton[1], 1, 1);

    gridLayout[2]->addWidget(itemLabel[2], 0, 0);
    gridLayout[2]->addWidget(itemLabel[5], 0, 1);
    gridLayout[2]->addWidget(itemLabel[8], 1, 0);
    gridLayout[2]->addWidget(switchButton[2], 1, 1);

    QListWidgetItem *listWidgetItem[3];

    for (int i = 0; i < 3; i++) {
        listWidgetItem[i] = new QListWidgetItem();
        listWidget->addItem(listWidgetItem[i]);
    }

    widget[8]->setLayout(gridLayout[0]);
    widget[9]->setLayout(gridLayout[1]);
    widget[10]->setLayout(gridLayout[2]);
    listWidget->setItemWidget(listWidget->item(0), widget[8]);
    listWidget->setItemWidget(listWidget->item(1), widget[9]);
    listWidget->setItemWidget(listWidget->item(2), widget[10]);
}

void MainWindow::onToggled(bool flag) {
    SwitchButton *bt = (SwitchButton *)sender();
    int iError;
    if(bt == switchButton[0])
    {
        if(flag)
            led_data = 1;
        else
            led_data = 0;
    }
    iError = write(fd_led, &led_data, sizeof(led_data));
    if (iError < 0)
    {
        printf("LED Control Failed!\r\n");
    }
//    for (int i = 0; i < 3; i++) {
//        if (bt == switchButton[i]) {
//            QStringList list = itemLabel[i + 6]->text().split("|");
//            QString message;
//            if (flag)
//                message = "开";
//            else
//                message = "关";
//            qDebug()<<message<<endl;
//            break;
//        }
//    }
}
void MainWindow::exitPushButtonCliked()
{
     qDebug()<<"QT is quit! "<<endl;
     QCoreApplication::quit();
}
void MainWindow::timerOut()
{
    int iError;
    int checksum;
    unsigned char ucBuf[5];
    iError = read(fd_dht11,ucBuf,sizeof(ucBuf));
//    if(iError!=sizeof(ucBuf))
//    {
//        qDebug() << "Failed to read dht11 device file: " << endl;
//    }
    // 验证校验和
    checksum = ucBuf[0] + ucBuf[1] + ucBuf[2] + ucBuf[3];
    if (checksum != ucBuf[4])
    {
        qDebug()<<"checksum is error!\r\n";
        return;
    }
    // 提取湿度和温度值
    humidity =  QString("%1.%2").arg(ucBuf[0]).arg(ucBuf[1], 2, 10, QLatin1Char('0'));
    temperature = QString("%1.%2").arg(ucBuf[2]).arg(ucBuf[3], 2, 10, QLatin1Char('0'));
    label[6]->setText(temperature);
    label[8]->setText(humidity);
}
